package  
{
	import engine.data.ModelLocator;
	import engine.display.Optimize;
	import engine.logic.event.LogicEvent;
	import engine.net.mgr.SystemReturn;
	import engine.net.mgr.SystemSender;
	import engine.ui.lite.BaseUI;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.Capabilities;
	import flash.system.Security;
	import flash.ui.ContextMenu;
	import flash.utils.Timer;
	
	import newx.StageInfo;
	import newx.com.LayoutType;
	import newx.com.config.StaticIni;
	import newx.com.loader.SwfLoader;
	import newx.utils.Stats;
	import newx.utils.debug.DebugWindow;
	import newx.utils.debug.IO;
	
	import toolNet.*;
	
	[SWF(frameRate="12", width="1280", height="768", backgroundColor="0x000000")]
	public class Jxjh extends Sprite
	{ 
		/**
		 * */
		private static var _urlFunc:Function;
		
		private var myContextMenu:ContextMenu;
		
		private var _time : Timer;
		
		public function Jxjh()
		{
			super();	
			if(stage == null)
			{
				addEventListener(Event.ADDED_TO_STAGE,initHandle);
			}
			else
			{
				initHandle(null)
			}
		}
		/**传递接口*/
		public function initLoad(loadfunc:Function):void
		{
			SwfLoader.setCustLoad(loadfunc);
		}
		
		public function initLoad2(fun:Function):void
		{
			SwfLoader.isTest	=	true;
			SwfLoader.setUnEncryptByte(fun);
		}
		
		/**
		 * 从NXloader传来的进度条
		 * 
		 */
		public function setConfig(loadbar:DisplayObject):void
		{
			StageInfo.loadingbar = loadbar;
		}
		
		public function initUrlConfig(func:Function):void
		{
			_urlFunc	=	func;			
		}
		
		/**获取文件真实地址*/
		public static function getUrl(str:String):String
		{
			if(_urlFunc == null) return str;
			
			return _urlFunc(str);
		}
		
//		/**获取地址*/
//		public function getTrue(url:String):String
//		{
//			return _urlFunc(url);
//		}
		
		
		
		private function initHandle(e:Event):void
		{
			
			NxLoaderProxy.init(stage);//首先执行，获取代理加载进度条，一定要在游戏清空舞台之前
			
			myContextMenu = new ContextMenu();
			
			myContextMenu.hideBuiltInItems();
			
//			this.contextMenu = myContextMenu;
			StageInfo.mainSpace.contextMenu = myContextMenu;
			
			if(e != null)
			{
				removeEventListener(Event.ADDED_TO_STAGE,initHandle);
			}
			
			//设置舞台
			StageInfo.InitStage(stage,1200,600,1400,750,"bgicon.jpg",LayoutType.CENTER);
			
			StageInfo.Clear([this]);
			
			StageInfo.params	=	loaderInfo.parameters;
			
			StageInfo.version	=	"0.1";
			
//			Security.loadPolicyFile("crossdomain.xml");
			
			Security.allowDomain("*");
			
			ModelLocator.getInstance().addEventListener(LogicEvent.INIT_COMPLETE,onGameInitComplete);
			
			NxLoaderProxy.show(StageInfo.systemSpace);//舞台初始化完成后后立即显示
			
//			StageInfo.params.debug	=	"true";
//			StageInfo.params.tool	=	"debug";
			
			//设置调试器
			if(StageInfo.params.debug == "true")
			{
//				DebugWindow.InitDebug(StageInfo.systemSpace);
				DebugWindow.enabled		=	true;
				DebugWindow.outMessage	=	true;
				
				var stats:Stats			=	new Stats();
				stats.x					=	0;
				stats.y					=	0;
				stats.mouseChildren		=	stats.mouseEnabled	=	false;
				StageInfo.systemSpace.addChild(stats);
			}
			else
			{
				//DebugWindow.enabled		=	false;
				//DebugWindow.outMessage	=	false;
//				stage.showDefaultContextMenu = false
			}
			
			if(StageInfo.params.hasOwnProperty("tool"))
			{
				SystemReturn._isShowLog = true;
				Model.getInstance().connect(StageInfo.params.tool);
			}
			stage.addChild(new NewxEngine());
		}
		
		private function onGameInitComplete(e:LogicEvent):void
		{
			DebugWindow.DebugCommandFun = onCommands;
			if(StageInfo.params.debug == "true")
			{
//				new GameTest()
			}
		}
		
		private function onCommands(str:String):void
		{
			if(str.search("showui ")>-1)
			{
				var className	:String	=	str.replace("showui ","");
				var ui			:BaseUI	=	StaticIni.getClipClass(
					StageInfo.stage.loaderInfo,
					"engine.newUi."+className).getInstance();
				ui.show();
			}
			if(str.search("sc")>-1)
			{
				IO.traceLog("Stage:",getMCCount(StageInfo.stage));
			}
			else if(str.search("playerc")>-1)
			{
//				IO.traceLog("playerc:",MainUI.getInstance().playerList.dataProvider.length);
			}
			else if(str.search("playerv")>-1)
			{
				IO.traceLog("\t当前客户端游戏版本\t\t:\t"		,"vCH20111115");
				IO.traceLog("\t当前Flash播放器版本\t\t:\t"	,Capabilities.version);
				IO.traceLog("\t当前Flash播放器类型\t\t:\t"	,Capabilities.playerType);
				IO.traceLog("\t当前计算机版本\t\t\t:\t"		,Capabilities.os);
				IO.traceLog("\t当前系统语言\t\t\t:\t"			,Capabilities.language);
				IO.traceLog("\t当前屏幕分辨率\t\t\t:\t"		,Capabilities.screenResolutionX
				+"×"+Capabilities.screenResolutionY+"\n");
			}
			else if(str == "gc")
			{
				IO.GC();
			}
			else if(str == "stopmovie")
			{
				Optimize.stopMovieClip(StageInfo.stage);
			}
			else if(str	.indexOf("set frameRate ")	==	0 )
			{
				var fr:int	=	int(str.substr(String("set frameRate ").length));
				StageInfo.stage.frameRate	=	fr;
				IO.traceLog(str," success"," frameRate:",fr);
			}
			else
			{
				SystemSender.getInstance().SendGMCommand(str);
			}		
		}
		
		private function getMCCount(mc:DisplayObject):int
		{
			var tConut:uint = 0;
			
			if(!mc.hasOwnProperty("numChildren"))
			{
				return 1;
			}
			
			var tSize:uint	=	mc["numChildren"];
			
			if(tSize == 0)
			{
				return 1;
			}
			
			for(var i:int = 0 ; i < tSize ; i ++)
			{
				tConut+=getMCCount(mc["getChildAt"](i))
			}
			return tConut;
		}
	}
}